package project.userFile;


import javax.servlet.*;
import javax.servlet.http.*;

import org.json.JSONException;
import org.json.JSONObject;
import project.Dao.UserDao;
import project.Domain.User;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.lang.System.out;


public class userFile extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("执行了doGet");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("执行了doPost");
    }

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("执行了service");
        try {
            processQueryFile(request, response);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    protected void processQueryFile(HttpServletRequest request, HttpServletResponse response) throws  IOException, JSONException {
//        设置响应消息编码格式
        response.setCharacterEncoding("utf-8");
        response.setHeader("Cache-Control", "no-cache");
        request.setCharacterEncoding("UTF-8");
        String user_role = request.getParameter("user_role");
        String id = request.getParameter("user_id");
        //      创建userDao对象，用于查询
        UserDao UserDao = new UserDao();
        List<User> userList = new ArrayList<>();
        switch (user_role){
            case "组长":
//                查询该组所有成员
                userList = UserDao.getUserList(Integer.parseInt(id));
                break;
            case "管理员":
//                查询管理员所管理的所有组长
                List<User> list = UserDao.getUserList(Integer.parseInt(id));
                for (User user : list) {
//                    通过组长id获取小组成员
                    List<User> grouper = new ArrayList<>();
                    grouper.add(user);
                    List<User> members = UserDao.getUserList(user.getId());
                    grouper.addAll(members);
                    userList.addAll(grouper);
                }/*
                //暂未入组的组员
                List<User> members = UserDao.getUserList(0);
                userList.addAll(members);*/
                break;
        }
        out.println(userList);
//      创建List 对象，封装数据返回
        List jsonList = new ArrayList();
        for (User user : userList) {
            Map map = new HashMap();
            map.put("id", user.getId());
            map.put("user_name", user.getName());
            map.put("user_email", user.getEmail());
            map.put("user_mobile", user.getMobile());
            map.put("user_role", user.getRole());
            map.put("leaderID", user.getLeaderID());
            jsonList.add(map);
        }
        JSONObject json = new JSONObject();
        json.put("user_list", jsonList);
        json.put("result_msg", "ok");
        json.put("result_code", 0);
        out.println("最后构造得到的json是:" + json);
        response.setContentType("text/html; charset=UTF-8");
        try {
            response.getWriter().print(json);
            response.getWriter().flush();
            response.getWriter().close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        out.println("返回结果给调用页面了。");
    }
}
